package com.hspedu.qqclient.service;

import com.hspedu.qqcommon.Message;
import com.hspedu.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.management.MemoryType;

/**
 * @author 王先锋
 * @version 1.0
 * 改类，提供和消息的相关方法
 */
public class MessageClientService {

    public  void sendMessageToAll(String content, String senderId){
        //构建message对象
        Message message = new Message();
        message.setSender(senderId);
        message.setContent(content);
        message.setMesType(MessageType.MESSAGE_TO_ALL_MES);
        message.setSendTime(new java.util.Date().toString());//发送时间
        System.out.println(senderId + "对大家说" + content);
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param content  内容
     * @param senderId 发送用户
     * @param getterId 接收用户
     */
    public void sendMessageToOne(String content, String senderId, String getterId) {
        //构建message对象
        Message message = new Message();
        message.setSender(senderId);
        message.setGetter(getterId);
        message.setContent(content);
        message.setMesType(MessageType.MESSAGE_COMM_MES);
        message.setSendTime(new java.util.Date().toString());//发送时间
        System.out.println(senderId + "对" + getterId + "说" + content);
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
